package gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DatabasePanel extends JFrame {
    private JPanel dbPanel;

    public DatabasePanel() {
        // 创建窗口
        setTitle("数据库设置");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // 禁止直接关闭窗口

        // 创建和配置dbPanel
        dbPanel = new JPanel(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();

        // 初始化约束条件
        constraints.anchor = GridBagConstraints.WEST; // 左对齐
        constraints.insets = new Insets(5, 5, 5, 5); // 组件边距
        constraints.fill = GridBagConstraints.HORIZONTAL; // 水平填充

        // 下拉框
        constraints.gridx = 0;
        constraints.gridy = 0;
        dbPanel.add(new JLabel("选择数据库类型:"), constraints);

        // 将面板添加到窗口
        add(dbPanel);

        // 添加窗口关闭监听器
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int confirmExit = JOptionPane.showConfirmDialog(
                        null,
                        "您确定要退出吗？",
                        "退出确认",
                        JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE
                );
                if (confirmExit == JOptionPane.YES_OPTION) {
                    int confirmSave = JOptionPane.showConfirmDialog(
                            null,
                            "是否需要保存设置？",
                            "保存设置",
                            JOptionPane.YES_NO_CANCEL_OPTION,
                            JOptionPane.QUESTION_MESSAGE
                    );
                    if (confirmSave == JOptionPane.YES_OPTION) {
                        // 执行保存设置的代码
                        System.out.println("保存设置。");
                    }
                    // 如果用户选择不保存或取消，直接关闭窗口
                    dispose();
                }
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(DatabasePanel::new);
    }
}